package csc.traning.wpsj.servlet;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import csc.traning.wpsj.dao.User;

/**
 * Servlet implementation class DeleteFileServlet
 */
public class DeleteFileServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static String DESTINATION_DIR_PATH ="";
	private File destinationDir;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeleteFileServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		if(user == null){
			throw new ServletException("Required to login");
		}
		
		String[] files = request.getParameterValues("filename");
		if(files == null || files.length == 0){
			response.sendRedirect("file-management");
			return;
		}
		
		String currentFolder = request.getParameter("currentFolder");
		
		DESTINATION_DIR_PATH = "users/" + user.getUserName() + "/upload-folder";
		
		if(currentFolder!=null && !currentFolder.equals("")){
			 String[] folers =  currentFolder.split("_");
            String currentFolderPath = "";
			 for (String folder : folers) {
           	 currentFolderPath = new StringBuffer(currentFolderPath).append("/").append(folder).toString();
            }
			 DESTINATION_DIR_PATH = DESTINATION_DIR_PATH + "/" + currentFolderPath;
		}else{
			currentFolder = "";
		}
		
		String realUploadPath = getServletContext().getRealPath(DESTINATION_DIR_PATH);
		//destinationDir = new File(realUploadPath);
		for (String file : files) {
			destinationDir = new File(realUploadPath + "/" + file);
			if(destinationDir.exists()){
				deleteFolder(destinationDir);
			}
			
		}
		
		if(currentFolder.length() > 1){
			currentFolder.substring(0, currentFolder.length()-2);
		}
		response.sendRedirect("file-management?folder="+currentFolder);
		return;
		
	}
	
	public static void deleteFolder(File folder) {
	    File[] files = folder.listFiles();
	    if(files!=null) { //some JVMs return null for empty dirs
	        for(File f: files) {
	            if(f.isDirectory()) {
	                deleteFolder(f);
	            } else {
	                f.delete();
	            }
	        }
	    }
	    folder.delete();
	}

}
